<template>
  <div class="addBpm">
    <Form :model="bpmModel" ref="form" :rules="ruleValidate" :label-width="100">
      <Form-item label="流程名称" prop="name" :label-width="150">
        <Input type="text" v-model="bpmModel.name" placeholder="请输入流程名称"></Input>
        <!-- 避免回车按钮导致提交，出现页面跳转 -->
        <input id="hiddenText" type="text" style="display: none;"/>
      </Form-item>
    </Form>
  </div>
</template>

<script>
import bpmpCenterApi from '@/api/bpmCenter.js'
import _ from 'lodash'
export default {
  components: {},
  data() {
    return {
      loading: false,
      bpmModel: {
        bpmpId: '',
        name: ''
      },
      ruleValidate: {
        name: [{ required: true, message: '请输入流程名称', trigger: 'blur' }]
      },
      actions: [
        {
          text: '确定',
          theme: 'primary',
          handle: () => {
            this.onOk()
          },
          loading: this.loading
        },
        {
          text: this.$t('common.close'),
          handle: () => {
            this.$emit('Close')
          }
        }
      ]
    }
  },
  props: {
    row: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  created() {
    this.onOk = _.debounce(this.onOk, 500)
    this.$emit('SetTitle', '复制流程')
    this.$emit('SetPageMinHeight', 150)
    this.$emit('SetPageActions', this.actions)
  },
  mounted() {
    this.bpmModel.bpmpId = this.row.id
  },
  methods: {
    onOk() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          bpmpCenterApi
            .bpmpCopy(this.bpmModel)
            .then(res => {
              this.loading = false
              if (res.data.code == 200) {
                this.$Message.success({
                  content: '复制成功',
                  duration: 3
                })
                this.$emit('Close', this.bpmModel)
              }
            })
            .catch(res => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
